package com.goethe.translator;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import com.goethe.translator.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.Service;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class Translator extends Activity {
	public final static String WORDTOTRANSLATE = "com.goethe.translator.MESSAGE";
	public final static String TARGETLANGUAGE = "com.goethe.translator.TARGETLANGUAGE";
	public final static String ORIGINALLANGUAGE = "com.goethe.translator.ORIGINALLANGUAGE";
	public final static int SPANISH_WORD = 0;
	public final static int SPANISH_ARTICLE = 1;
	public final static int SPANISH_PLURAL = 2;
	public final static int GERMAN_WORD = 3;
	public final static int GERMAN_ARTICLE = 4;
	public final static int GERMAN_PLURAL = 5;
	public final static int WORD_TYPE = 6;
	public final static int WORD_GROUP = 7;
	public final static int WORD_LEVEL = 8;
	private String targetLanguage = "";
	private String originalLanguage = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translator);
        
        Spinner spinner = (Spinner) findViewById(R.id.language_spinner);
	    // Create an ArrayAdapter using the string array and a default spinner layout
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.language_array, android.R.layout.simple_spinner_item);
	    // Specify the layout to use when the list of choices appears
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    // Apply the adapter to the spinner
	    spinner.setAdapter(adapter);
	    // Set the OnItemSelectedListener
	    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
	        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
	        	if(position==0){
	        		targetLanguage = "german";
	        		originalLanguage = "spanish";
	        	}else if(position==1){
	        		targetLanguage = "spanish";
	        		originalLanguage = "german";
	        	}
	        }

	        public void onNothingSelected(AdapterView<?> parentView) {
	            // your code here
	        }

	    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_translator, menu);
        return true;
    }
    
    // Called when the user clicks the "Traducir" button
    public void translate(View view) {
    	// We get the information contained in the edit text box
    	EditText editText = (EditText) findViewById(R.id.edit_message);
    	String key = editText.getText().toString();
    	
    	// We take the soft keyboard out of the screen
    	InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
    	imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); 
        
        // We make the http request and wait for the results
        TranslatorHttpRequest httpRequest = new TranslatorHttpRequest();
        httpRequest.execute(key, targetLanguage, originalLanguage);
        ArrayList<String> response = new ArrayList<String>();
        
		try {
			response = httpRequest.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
        
        if(response.isEmpty()){
	        // We set visibility for the views
        	findViewById(R.id.connectionFailure).setVisibility(android.view.View.VISIBLE);
	        findViewById(R.id.wordNotFound).setVisibility(android.view.View.GONE);
	        findViewById(R.id.firstRow).setVisibility(android.view.View.GONE);
	        findViewById(R.id.firstSeparator).setVisibility(android.view.View.GONE);
	        findViewById(R.id.secondRow).setVisibility(android.view.View.GONE);
	        findViewById(R.id.secondSeparator).setVisibility(android.view.View.GONE);
	        findViewById(R.id.thirdRow).setVisibility(android.view.View.GONE);
	        findViewById(R.id.fourthRow).setVisibility(android.view.View.GONE);
	        findViewById(R.id.fifthRow).setVisibility(android.view.View.GONE);
	        findViewById(R.id.thirdSeparator).setVisibility(android.view.View.GONE);

        }else{
	        if(!response.get(0).equals("")){
		        // We set visibility for the views
	        	findViewById(R.id.connectionFailure).setVisibility(android.view.View.GONE);
		        findViewById(R.id.wordNotFound).setVisibility(android.view.View.GONE);
		        findViewById(R.id.firstRow).setVisibility(android.view.View.VISIBLE);
		        findViewById(R.id.firstSeparator).setVisibility(android.view.View.VISIBLE);
		        findViewById(R.id.secondRow).setVisibility(android.view.View.VISIBLE);
		        findViewById(R.id.secondSeparator).setVisibility(android.view.View.VISIBLE);
		        findViewById(R.id.thirdRow).setVisibility(android.view.View.VISIBLE);
		        findViewById(R.id.fourthRow).setVisibility(android.view.View.VISIBLE);
		        findViewById(R.id.fifthRow).setVisibility(android.view.View.VISIBLE);
		        findViewById(R.id.thirdSeparator).setVisibility(android.view.View.VISIBLE);
		        
		      	// Set the first row - contains the languages
		        TextView text = (TextView) findViewById(R.id.translatorTableText1a);
		        text.setText(getString(R.string.spanish));
		        text = (TextView) findViewById(R.id.translatorTableText1b);
		        text.setText(getString(R.string.german));
		
		      	// Set the second row - contains the actual words
		        text = (TextView) findViewById(R.id.translatorTableText2a);
		        text.setText(response.get(SPANISH_ARTICLE) + " " + response.get(SPANISH_WORD) + " (" + response.get(SPANISH_PLURAL) + ")");
		        text = (TextView) findViewById(R.id.translatorTableText2b);
		        text.setText(response.get(GERMAN_ARTICLE) + " " + response.get(GERMAN_WORD) + " (" + response.get(GERMAN_PLURAL) + ")");     
		      	
		        // Set the third row - contains the word type
		        text = (TextView) findViewById(R.id.translatorTableText3);
		        text.setText(getString(R.string.word_type) + ": " + response.get(WORD_TYPE));
		        
		        // Set the fourth row - contains the word group
		        text = (TextView) findViewById(R.id.translatorTableText4);
		        text.setText(getString(R.string.word_group) + ": " + response.get(WORD_GROUP));
		        
		        // Set the fifth row - contains the word level
		        text = (TextView) findViewById(R.id.translatorTableText5);
		        text.setText(getString(R.string.word_level) + ": " + response.get(WORD_LEVEL));
	        }else{
		        // We set visibility for the views
	        	findViewById(R.id.connectionFailure).setVisibility(android.view.View.GONE);
		        findViewById(R.id.wordNotFound).setVisibility(android.view.View.VISIBLE);
		        findViewById(R.id.firstRow).setVisibility(android.view.View.GONE);
		        findViewById(R.id.firstSeparator).setVisibility(android.view.View.GONE);
		        findViewById(R.id.secondRow).setVisibility(android.view.View.GONE);
		        findViewById(R.id.secondSeparator).setVisibility(android.view.View.GONE);
		        findViewById(R.id.thirdRow).setVisibility(android.view.View.GONE);
		        findViewById(R.id.fourthRow).setVisibility(android.view.View.GONE);
		        findViewById(R.id.fifthRow).setVisibility(android.view.View.GONE);
		        findViewById(R.id.thirdSeparator).setVisibility(android.view.View.GONE);
	        }
        }
    }
}
